package Pre.Work;

import java.util.*;

public class GameRoom {
    private List list1 = new ArrayList();
    private List list2 = new ArrayList();
    private List list3 = new ArrayList();
    private List cardlist = new ArrayList();
    public void cord() {
        cardlist.clear();
        String[] colors = {"♣", "♠", "♦", "♥"};
        String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        for (int i = 0; i < 4; i++) {
            String color = colors[i];
            for (int j = 0; j < 13; j++) {
                Card card = new Card(num[j], color, j + 1);
                cardlist.add(card);
            }
        }
        Card dw = new Card("", "🃏", 100);
        Card xw = new Card("", "🤡", 50);
        Collections.addAll(cardlist, dw, xw);
    }

    public void dealCards() {
        list1.clear();
        list2.clear();
        list3.clear();
        Collections.shuffle(cardlist);
        List[] player = new List[]{list1, list2, list3};
        for (int i = 0; i < player.length; i++) {
            List list = player[i];
            int count = 0;
            Iterator iterator = cardlist.iterator();
            while (iterator.hasNext() && count < 17) {
                Card card = (Card) iterator.next();
                list.add(card);
                iterator.remove();
                count++;
            }
            list.sort(new PokerComparator());
            System.out.print("玩家" + (i + 1) + "的牌：");
            list.forEach(obj -> System.out.print(obj));
            System.out.println();
        }
    }

    public void showLastCard() {
        cardlist.sort(new PokerComparator());
        System.out.println("\n" + "底牌为：" + cardlist);
    }

    class PokerComparator implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Card c1 = (Card) o1;
            Card c2 = (Card) o2;
            return c1.getSize() - c2.getSize();
        }
    }
}

    class Test {
        static GameRoom gr = new GameRoom();
        static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) {
            while (true){
                menu();
            }
        }

        public static  void menu(){
            System.out.println("********欢乐斗地主*********");
            System.out.println("1.开始游戏");
            System.out.println("2.发牌");
            System.out.println("3.展示底牌");
            System.out.println("4.重新发牌");
            System.out.println("0.退出游戏");
            System.out.println("**************************");
            int type = sc.nextInt();
            switch (type){
                case 1 -> gr.cord();
                case 2 -> {
                    gr.cord();
                    gr.dealCards();
                }
                case 3 -> gr.showLastCard();
                case 4 -> {
                    gr.cord();
                    gr.dealCards();
                }
                case 0 -> System.exit(1);
                }
            }
        }
